import tkinter as tk
from tkinter import ttk
from typing import Callable
from .model_settings import ModelSettings
from .role_settings import RoleSettings


class SettingsTab:
    """设置界面主标签页"""
    
    def __init__(self, parent_frame, models: dict, config_manager=None):
        self.parent_frame = parent_frame
        self.models = models
        self.config_manager = config_manager
        
        # 子设置界面
        self.model_settings = None
        self.role_settings = None
        
        # 回调函数
        self.on_model_change = None
        self.on_role_change = None
        self.on_save_settings = None
        
        self.setup_settings_interface()
    
    def setup_settings_interface(self):
        """设置设置界面"""
        # 创建标签页控件
        self.notebook = ttk.Notebook(self.parent_frame)
        self.notebook.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        
        # 模型设置标签页
        self.model_frame = ttk.Frame(self.notebook)
        self.notebook.add(self.model_frame, text="模型设置")
        self.model_settings = ModelSettings(self.model_frame, self.config_manager, self.models)
        
        # 角色设置标签页
        self.role_frame = ttk.Frame(self.notebook)
        self.notebook.add(self.role_frame, text="角色设置")
        self.role_settings = RoleSettings(self.role_frame, self.config_manager)
        
        # 设置子界面的回调函数
        self.setup_sub_callbacks()
    
    def setup_sub_callbacks(self):
        """设置子界面的回调函数"""
        if self.model_settings:
            self.model_settings.set_callbacks(
                on_model_change=self._on_model_change,
                on_save_settings=self._on_save_model_settings
            )
        
        if self.role_settings:
            self.role_settings.set_callbacks(
                on_role_change=self._on_role_change,
                on_save_settings=self._on_save_role_settings
            )
    
    def get_knowledge_base_for_role(self, role: str):
        """获取指定角色的知识库"""
        if self.role_settings:
            return self.role_settings.get_knowledge_base_for_role(role)
        return None
    
    def get_settings(self) -> dict:
        """获取所有设置"""
        settings = {}
        
        # 获取模型设置
        if self.model_settings:
            model_settings = self.model_settings.get_settings()
            settings.update(model_settings)
        
        # 获取角色设置
        if self.role_settings:
            role_settings = self.role_settings.get_settings()
            settings.update(role_settings)
        
        return settings
    
    def load_settings(self, settings: dict):
        """加载设置"""
        # 加载模型设置
        if self.model_settings:
            self.model_settings.load_settings(settings)
        
        # 加载角色设置
        if self.role_settings:
            self.role_settings.load_settings(settings)
    
    def set_callbacks(self, on_model_change: Callable[[], None], on_role_change: Callable[[], None], 
                     on_save_settings: Callable[[], None]):
        """设置回调函数"""
        self.on_model_change = on_model_change
        self.on_role_change = on_role_change
        self.on_save_settings = on_save_settings
    
    def _on_model_change(self):
        """模型改变事件"""
        if self.on_model_change:
            self.on_model_change()
    
    def _on_role_change(self):
        """角色改变事件"""
        if self.on_role_change:
            self.on_role_change()
    
    def _on_save_model_settings(self):
        """保存模型设置事件"""
        if self.on_save_settings:
            self.on_save_settings()
    
    def _on_save_role_settings(self):
        """保存角色设置事件"""
        if self.on_save_settings:
            self.on_save_settings()